package cn.v2tila.rsosa.student.controller;

import cn.v2tila.rsosa.student.entity.AssoEntity;
import cn.v2tila.rsosa.student.entity.CollegeEntity;
import cn.v2tila.rsosa.student.entity.UserEntity;
import cn.v2tila.rsosa.student.service.*;
import cn.v2tila.rsosa.student.vo.AssoInfo;
import cn.v2tila.rsosa.student.vo.AssoPicker;
import cn.v2tila.rsosa.student.vo.RequestVO;
import cn.v2tila.rsosa.student.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author v2tila on 20:26 2019/2/27
 */
@RestController
@RequestMapping(value = "asso")
public class assoController {
    @Autowired
    private AssoService assoService;
    @Autowired
    private CollegeService collegeService;
    @Autowired
    private SchoolService schoolService;
    @Autowired
    private UserService userService;
    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "getAssoPicker")
    public ResultVO getAssoPicker(RequestVO requestVO){
        ResultVO resultVO = new ResultVO();
        List<AssoPicker> assoPickers = new ArrayList<AssoPicker>();
        AssoPicker assoPicker = new AssoPicker();
        List<String> assoNames = new ArrayList<String>();
        String schoolName = requestVO.getSchoolName();
        String schoolNo = schoolService.findByName(schoolName).getNo();
        List<CollegeEntity> collegeList = collegeService.findBySchoolNo(schoolNo);

        assoPicker.setCollege("请选择");
        assoPicker.setId(0);
        assoNames.add("请选择社团名称");
        assoPicker.setAssoNames(assoNames);
        System.out.println(collegeList.get(0));
        assoPickers.add(0,assoPicker);
        System.out.println(assoPickers);
        for (int i = 0; i < collegeList.size(); i++) {
            AssoPicker assoPicker1 = new AssoPicker();
            String collegeName = collegeList.get(i).getName();
            String collegeNo = collegeList.get(i).getNo();
            List<AssoEntity> assoEntities = assoService.findBySchoolNoAndAndCollegeNo(schoolNo,collegeNo);
            List<String> assoNames1 = new ArrayList<String>();
            for(int j =0;j<assoEntities.size();j++){
                assoNames1.add(assoEntities.get(j).getName());
            }
            assoPicker1.setCollege(collegeName);
            assoPicker1.setId(i+1);
            assoPicker1.setAssoNames(assoNames1);
            assoPickers.add(i+1,assoPicker1);
        }
        System.out.println(assoPickers);
        resultVO.setAssoPickers(assoPickers);
        return resultVO;
    }

    @RequestMapping(value = "register")
    public ResultVO register(RequestVO requestVO){
        ResultVO resultVO = new ResultVO();
        String schoolName = requestVO.getSchoolName();
        String assoName = requestVO.getAssoName();
        String password = requestVO.getPassword();
        String schoolNo = schoolService.findByName(schoolName).getNo();
        AssoEntity assoEntity = assoService.findBySchoolNoAndName(schoolNo, assoName);
        if(!assoEntity.getPassword().equals(password)){
            resultVO.setCode(0);
            resultVO.setMsg("密码错误");
        }else{
            if(assoEntity.getUniqueId()==null){
                String openid = requestVO.getOpenid();
                String uniqueId = requestVO.getUniqueId();
                assoEntity.setUniqueId(uniqueId);
                AssoEntity saveAndFlush = assoService.saveAndFlush(assoEntity);
                if(saveAndFlush == null ){
                    resultVO.setCode(0);
                    resultVO.setMsg("数据储存错误，请联系管理员");
                }else{
                    UserEntity userEntity = userService.findByOpenid(openid);
                    userEntity.setIsAsso(1);
                    UserEntity saveAndFlush1 = userService.saveAndFlush(userEntity);
                    if (saveAndFlush1 == null) {
                        resultVO.setCode(0);
                        resultVO.setMsg("数据储存错误，请联系管理员");
                    }else{
                        resultVO.setCode(1);
                        resultVO.setMsg("绑定成功");
                    }
                }
            }else{
                resultVO.setCode(0);
                resultVO.setMsg("该社团已有管理员");
            }
        }
        return resultVO;
    }

    @RequestMapping(value = "getAssoByStuNo")
    public ResultVO getAssoByStuNo(RequestVO requestVO) {
        ResultVO resultVO = new ResultVO();
        String uniqueId = requestVO.getUniqueId();
        AssoEntity assoEntity = assoService.findByUniqueId(uniqueId);
        AssoInfo assoInfo = new AssoInfo();
        assoInfo.setAssoName(assoEntity.getName());
        assoInfo.setAssoNo(assoEntity.getNo());
        assoInfo.setAssoType(assoEntity.getAssoType());
        assoInfo.setSchoolName(schoolService.findByNo(assoEntity.getSchoolNo()).getName());
        assoInfo.setCollegeName(collegeService.findByNo(assoEntity.getCollegeNo()).getName());
        assoInfo.setMgtName(studentService.findByUniqueId(assoEntity.getUniqueId()).getName());
        resultVO.setAssoInfo(assoInfo);
        return resultVO;
    }

    @RequestMapping(value = "getAssoId")
    public Integer getAssoId(RequestVO requestVO) {
        String uniqueId = requestVO.getUniqueId();
        String schoolNo = schoolService.findByName(requestVO.getSchoolName()).getNo();
        AssoEntity assoEntity = assoService.findBySchoolNoAndUniqueId(schoolNo,uniqueId);
        return assoEntity.getId();
    }

    @RequestMapping(value = "getIsPublic")
    public Integer getIsPublic(String uniqueId) {
        AssoEntity assoEntity = assoService.findByUniqueId(uniqueId);
        return assoEntity.getIsPublic();
    }
}
